package org.rufolfheszele.andpixelbyandpixel.configuration;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * This class is holding every network related configuration data
 *
 * @author Rudolf Heszele heszele@gmail.com
 * @version 1.0
 * @since 2012-03-08
 */
public class NetworkConfiguration
{
	/**
	 * The only constructor takes the XML node and reads every data into its data members
	 *
	 * @param node The XML node containing the network configuration
	 */
	public NetworkConfiguration(Node node)
	{
		NodeList children = node.getChildNodes();
		
		mNode = node;
		for(int i = 0; i < children.getLength(); ++i)
		{
			Node childNode = children.item(i);
			
			if(true == childNode.getNodeName().equals("port-number"))
				mPortNumber = Integer.parseInt(childNode.getChildNodes().item(0).getNodeValue());
			if(true == childNode.getNodeName().equals("data-sending-frequency"))
				mFrequency = Integer.parseInt(childNode.getChildNodes().item(0).getNodeValue());
			if(true == childNode.getNodeName().equals("only-free-servers"))
			{
				if(true == childNode.getChildNodes().item(0).getNodeValue().equals("yes"))
					mFetchOnlyFreeServers = true;
				else
					mFetchOnlyFreeServers = false;
			}
			if(true == childNode.getNodeName().equals("download-unknown-levels"))
			{
				if(true == childNode.getChildNodes().item(0).getNodeValue().equals("yes"))
					mDownloadUnknownLevels = true;
				else
					mDownloadUnknownLevels = false;
			}
			if(true == childNode.getNodeName().equals("upload-levels"))
			{
				if(true == childNode.getChildNodes().item(0).getNodeValue().equals("yes"))
					mUploadLevels = true;
				else
					mUploadLevels = false;
			}
			if(true == childNode.getNodeName().equals("register-server"))
			{
				if(true == childNode.getChildNodes().item(0).getNodeValue().equals("yes"))
					mRegisterServer = true;
				else
					mRegisterServer = false;
			}
			if(true == childNode.getNodeName().equals("proxy-settings"))
			{
				mProxyConfiguration = new ProxyConfiguration(childNode);
			}
				
		}
		mCachedFrequency = mFrequency;
	}
	
	public int getPortNumber()
	{
		return mPortNumber;
	}
	
	public void setPortNumber(int portNumber)
	{
		String nodeContent;

		mPortNumber = portNumber;
		nodeContent = Integer.toString(portNumber);
		Configuration.updateNodeContent(mNode, "port-number", nodeContent, null);
	}
	
	public int getFrequency()
	{
		return mCachedFrequency;
	}
	
	public void setFrequency(int frequency)
	{
		String nodeContent;

		mFrequency = frequency;
		mCachedFrequency = frequency;
		nodeContent = Integer.toString(frequency);
		Configuration.updateNodeContent(mNode, "data-sending-frequency", nodeContent, null);
	}
	
	/**
	 * Cashes the given frequency, but will not be written to the xml file
	 *
	 * @param frequency The new frequency to be cached
	 */
	public void cacheFrequency(int frequency)
	{
		mCachedFrequency = frequency;
	}
	
	/**
	 * Clears the cached frequency value
	 */
	public void resetCache()
	{
		mCachedFrequency = mFrequency;
	}
	
	public boolean fetchOnlyFreeServers()
	{
		return mFetchOnlyFreeServers;
	}
	
	public void fetchOnlyFreeServers(boolean fetchOnlyFreeServers)
	{
		String nodeContent;

		mFetchOnlyFreeServers = fetchOnlyFreeServers;
		if(true == fetchOnlyFreeServers)
			nodeContent = "yes";
		else
			nodeContent = "no";
		Configuration.updateNodeContent(mNode, "only-free-servers", nodeContent, null);
	}
	
	public boolean downloadUnknownLevels()
	{
		return mDownloadUnknownLevels;
	}
	
	public void downloadUnknownLevels(boolean downloadUnknownLevels)
	{
		String nodeContent;

		mDownloadUnknownLevels = downloadUnknownLevels;
		if(true == downloadUnknownLevels)
			nodeContent = "yes";
		else
			nodeContent = "no";
		Configuration.updateNodeContent(mNode, "download-unknown-levels", nodeContent, null);
	}
	
	public boolean uploadLevels()
	{
		return mUploadLevels;
	}
	
	public void uploadLevels(boolean uploadLevels)
	{
		String nodeContent;

		mUploadLevels = uploadLevels;
		if(true == uploadLevels)
			nodeContent = "yes";
		else
			nodeContent = "no";
		Configuration.updateNodeContent(mNode, "upload-levels", nodeContent, null);
	}
	
	public boolean registerServer()
	{
		return mRegisterServer;
	}
	
	public void registerServer(boolean registerServer)
	{
		String nodeContent;

		mRegisterServer = registerServer;
		if(true == registerServer)
			nodeContent = "yes";
		else
			nodeContent = "no";
		Configuration.updateNodeContent(mNode, "register-server", nodeContent, null);
	}
	
	public ProxyConfiguration getProxyConfiguration()
	{
		return mProxyConfiguration;
	}

	private int mPortNumber							= -1;
	private int mFrequency							= 0;
	private int mCachedFrequency					= 0;
	private boolean mFetchOnlyFreeServers			= false;
	private boolean mDownloadUnknownLevels			= false;
	private boolean mUploadLevels					= false;
	private boolean mRegisterServer					= false;
	private ProxyConfiguration mProxyConfiguration	= null;
	private Node mNode								= null;
}
